package com.yunfei.api.v1.service;

import com.google.gson.Gson;
import com.yunfei.api.v1.magerIndcInfo.service.MagerIndcInfoService;
import com.yunfei.api.v1.param.CancelContractMessageSendParam;
import com.yunfei.api.v1.param.ContractCancelParam;
import com.yunfei.api.v1.param.createZuoFeiXieYi.CreateZuoFeiXieYiJsonRootBean;
import com.yunfei.api.v1.param.createZuoFeiXieYi.ZuoFeiXieYiContractData;
import com.yunfei.api.v1.param.createZuoFeiXieYi.ZuoFeiXieYiTemplateReplaceKeyValueList;
import com.yunfei.api.v1.result.ZuoFeiXieYiResult;
import com.yunfei.api.v2.common.YunHeTongApiContant;
import com.yunfei.api.v2.elecHeTong.json.ContractTable;
import com.yunfei.api.v2.enums.CreateZuoFeiXieYiEnum;
import com.yunfei.api.v2.param.*;
import com.yunfei.api.v2.result.YuYueInfoResult;
import com.yunfei.api.v2.result.createTemplate.CreateTemplateJsonRootBean;
import com.yunfei.api.v2.result.createTemplate.Data;
import com.yunfei.api.v2.service.AddSignerService;
import com.yunfei.api.v2.service.GetLongTokenService;
import com.yunfei.api.v2.service.HTYuYueInfoService;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.HeTongSignInfoGetStatusResultStatus;
import com.yunfei.enums.ResultStatus;
import com.yunfei.mapper.aiiidb.*;
import com.yunfei.utils.http.CreateTemplateHttpUtil;
import com.yunfei.utils.string.StringUtil;
import com.yunfei.yunhetong.elecContCancelDtl.bo.ElecContCancelDtlBo;
import com.yunfei.yunhetong.elecContCancelDtl.service.ElecContCancelDtlService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CreateZuoFeiXieYiService {
    @Autowired
    private ZuoFeiXieYiMapper zuoFeiXieYiMapper;
    @Autowired
    private GetLongTokenService getLongTokenService;
    @Autowired
    private HeTongSignInfoMapper heTongSignInfoMapper;
    @Autowired
    private MagerIndcInfoService magerIndcInfoService;
    @Autowired
    private HTYuYueInfoService htYuYueInfoService;
    @Autowired
    private AccountInfoMapper getAccountInfoMapper;
    @Autowired
    private ElecContBeneAcceMapper elecContBeneAcceMapper;
    @Autowired
    private AddSignerService addSignerService;
    @Autowired
    private ElecContCancelDtlService elecContCancelDtlService;
    @Autowired
    private CancelContractMessageSendMapper cancelContractMessageSendMapper;
    //获取作废协议模板填充数据
    public ResultModel get(ReserveNoParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveNo=param.getReserveNo();//预约编号
            if(StringUtil.isNullOrEmpty(reserveNo)){
                resultModel.setCode(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getCode());
                resultModel.setMessage(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getMessage());
                return resultModel;
            }
            queryMap.put("reserveNo",reserveNo);
            ZuoFeiXieYiResult yuYueInfoResult=zuoFeiXieYiMapper.get(queryMap);
            resultModel.setData(yuYueInfoResult);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }

    /**
    * @Description: 生成作废协议
    * @Author: zhengyunfei
    * @Date: 2019/9/4
    * @time:15:24
    */
    public Object create(ResultModel resultModel, ReserveNoParam param,CurrentUserParam currentUserParam, ContractIdParam contractIdParam, ContractCancelParam contractCancelParam) {
        String reserveNo=param.getReserveNo();//预约编号
        String contractId=contractIdParam.getContractId();
        String currentUserId=currentUserParam.getCurrentUserId();
        String contractCancelRemark=contractCancelParam.getCancelRemark();
        Map<String,Object> queryMap=new HashMap<>();
        //参数校验
        if(StringUtil.isNullOrEmpty(contractCancelRemark)){
            resultModel.setCode(CreateZuoFeiXieYiEnum.CANCEL_CONTRACT_REMARK_NOTNULL.getCode());
            resultModel.setMessage(CreateZuoFeiXieYiEnum.CANCEL_CONTRACT_REMARK_NOTNULL.getMessage());
            return resultModel;
        }
        if(StringUtil.isNullOrEmpty(reserveNo)){
            resultModel.setCode(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getCode());
            resultModel.setMessage(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getMessage());
            return resultModel;
        }
        if(StringUtil.isNullOrEmpty(contractId)){
            resultModel.setCode(CreateZuoFeiXieYiEnum.CONTRACT_ID_NOTNULL.getCode());
            resultModel.setMessage(CreateZuoFeiXieYiEnum.CONTRACT_ID_NOTNULL.getMessage());
            return resultModel;
        }
        //根据预约编号，获取根据模板生成合同，生成作废协议所需要的参数
        queryMap.put("reserveNo",reserveNo);
        ZuoFeiXieYiResult zuoFeiXieYiResult=zuoFeiXieYiMapper.get(queryMap);
        if(!StringUtil.isNullOrEmpty(zuoFeiXieYiResult)){
            //封装调用根据模板生成合同接口，生成作废协议的json数据
            CreateZuoFeiXieYiJsonRootBean createElecHtJsonRootBean=new CreateZuoFeiXieYiJsonRootBean();
            //createElecHtJsonRootBean.setContractNo(contractId);//可选参数，不传时默认与合同 id 相同
           // createElecHtJsonRootBean.setContractTitle(zuoFeiXieYiResult.getCancelContractName());
            createElecHtJsonRootBean.setContractTitle(contractId);//合同标题，传合同编号
            createElecHtJsonRootBean.setTemplateId(zuoFeiXieYiResult.getCancelContractId());
            zuoFeiXieYiResult.setCanRemark(contractCancelParam.getCancelRemark());
            ZuoFeiXieYiContractData contractData= ZuoFeiXieYiContractData.getContractData(zuoFeiXieYiResult);
            createElecHtJsonRootBean.setContractData(contractData);
            //替换为模板需要的json数据格式
            Gson gson=new Gson();
            StringBuffer result= ZuoFeiXieYiTemplateReplaceKeyValueList.replace(gson.toJson(createElecHtJsonRootBean));
            //调用云合同生成电子合同的接口
            System.out.println("参数====="+result.toString());
            String  token=getLongTokenService.getLongToken(new LongTokenParam());
            String url= YunHeTongApiContant.CREATE_ELEC_CONTRACT_API;
            //调用api接口，根据模板生成合同，来生成作废协议
            JSONObject resultJsonObject= CreateTemplateHttpUtil.post(token,url,result.toString());
            System.out.println("生成作废协议返回========"+resultJsonObject);
            CreateTemplateJsonRootBean createTemplateJsonRootBean= (CreateTemplateJsonRootBean) JSONObject.toBean(resultJsonObject, CreateTemplateJsonRootBean.class);
            int code=createTemplateJsonRootBean.getCode();
            if(code==200){
                long cancelContractId=createTemplateJsonRootBean.getData().getContractId();
                System.out.println("生成作废协议，调用根据模板生成合同接口返回结果======================="+cancelContractId);
                //插入表
                ElecContCancelDtlBo elecContCancelDtlBo=new ElecContCancelDtlBo();
                elecContCancelDtlBo.setCancelContractId(cancelContractId+"");
                elecContCancelDtlBo.setCancelContractNum(cancelContractId+"");
                elecContCancelDtlBo.setContractSts("4");//合同状态默认4
                elecContCancelDtlBo.setCReserveno(reserveNo);
                elecContCancelDtlBo.setContractId(contractId);
                elecContCancelDtlBo.setCreatorId(currentUserId);
                elecContCancelDtlBo.setCancelRemark(contractCancelParam.getCancelRemark());
                elecContCancelDtlService.add(elecContCancelDtlBo);
                //添加签署者，传入的合同id，是生成作废协议模板返回后的id
                resultModel=addSignerService.addSignerForCreateZuoFeiXieYi(cancelContractId+"",reserveNo);
                resultModel.setCode(ResultStatus.SUCCESS.getCode());
                resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
                return createTemplateJsonRootBean;
            }else{
                String msg=createTemplateJsonRootBean.getMsg();
                resultModel.setCode(code);
                resultModel.setMessage(msg);
                Data data=createTemplateJsonRootBean.getData();
                resultModel.setData(data);
                return resultModel;
            }
        }else{
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage("此预约编号对应的 ELEC_CONT_BENE_ACCT_DTL表EFFE_FLG=N导致数据没有查询出来");
            return resultModel;
        }

    }
    /**
    * @Description: 再次生成作废协议-》推送消息
    * @Author: zhengyunfei
    * @Date: 2019/9/5
    * @time:16:24
    */
    public void sendMessage(String contractId,String reserveNo){
        //获取消息id
        String msgId=cancelContractMessageSendMapper.getMsgId();
        //存入消息
        CancelContractMessageSendParam addParam=new CancelContractMessageSendParam();
        addParam.setContractId(contractId);
        addParam.setReserveNo(reserveNo);
        addParam.setMsgId(msgId);
        cancelContractMessageSendMapper.add(addParam);
        //更新推送状态
        cancelContractMessageSendMapper.update(addParam);
    }
}
